//
//  main.m
//  TerminalApp
//
//  Created by ZhangZn on 2017/1/19.
//  Copyright © 2017年 Bizvane. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSString *errStr = @"0   CoreFoundation                      0x00000001822d25b8 <redacted> + 160,1   libobjc.A.dylib                     0x0000000192a300e4 objc_exception_throw + 60,2   CoreFoundation                      0x00000001822d245c <redacted> + 0,3   Foundation                          0x0000000183159554 <redacted> + 112,4   UIKit                               0x0000000186c8c290 <redacted> + 744,5   UIKit                               0x0000000186c8a09c <redacted> + 164,6   UIKit                               0x0000000186c89aec <redacted> + 1660,7   UIKit                               0x0000000186c6db00 <redacted> + 10020,8   iShow                               0x0000000100246120 iShow + 1974560,9   UIKit                               0x0000000186bacca4 <redacted> + 1280,10  UIKit                               0x0000000186c6a5f0 <redacted> + 276,11  UIKit                               0x0000000186b06e80 <redacted> + 356,12  UIKit                               0x0000000186a78794 <redacted> + 532,13  CoreFoundation                      0x000000018228aa50 <redacted> + 32,14  CoreFoundation                      0x00000001822879dc <redacted> + 360,15  CoreFoundation                      0x0000000182287dbc <redacted> + 836,16  CoreFoundation                      0x00000001821b50a4 CFRunLoopRunSpecific + 396,17  GraphicsServices                    0x000000018b3575a4 GSEventRunModal + 168,18  UIKit                               0x0000000186aea3c0 UIApplicationMain + 1488,19  iShow                               0x000000010019dadc iShow + 1284828,20  libdyld.dylib                       0x000000019309ea08 <redacted> + 4";
        errStr = [errStr stringByReplacingOccurrencesOfString:@" " withString:@""];

        NSArray *errArray = [errStr componentsSeparatedByString:@","];
        NSString *path = @"/Users/ZhangZn/Desktop/iShow-2.3.20";//文件路径
        
        for (int i = 0; i<errArray.count; i++) {
            
            NSString *errStrIndex = errArray[i];
            
            NSRange headRange = [errStrIndex rangeOfString:@"0x000"];
            
            errStrIndex = [errStrIndex substringFromIndex:headRange.location];
//            NSString *result = @"0x00000001000a2438";//错误地址
//            long int cutNum = 222264;
            
            NSString *result = [errStrIndex substringToIndex:18];
            
            
            
            NSRange numberRange = [errStrIndex rangeOfString:@"+"];
            
            NSString *numberStr = [errStrIndex substringWithRange:NSMakeRange(numberRange.location+1, errStrIndex.length-numberRange.location-1)];
            
            long int cutNum = [numberStr intValue];
            
            if ([result containsString:@"0x0000000"]) {
                result = [result stringByReplacingOccurrencesOfString:@"0x0000000" withString:@""];
            }
            if ([result containsString:@"0x"]) {
                result = [result stringByReplacingOccurrencesOfString:@"0x" withString:@""];
            }
            NSString *temp = [NSString stringWithFormat:@"%lu",strtoul([result UTF8String], 0, 16)];
            
            long int baseValue = [temp longLongValue];
            
            long int errorInt = baseValue-cutNum;
            
            NSString *hexString = [NSString stringWithFormat:@"%@",[[NSString alloc] initWithFormat:@"%1lx",errorInt]];//错误基地址
            
            NSString *terminalCode = [NSString stringWithFormat:@"atos -arch armv7 -o %@ -l 0x%@ 0x%@",path,hexString,result];
//            NSLog(@"%@",terminalCode);
            
            NSTask *task;
            task = [[NSTask alloc] init];
            [task setLaunchPath: @"/bin/bash"];
            
            NSArray *arguments;
            arguments = [NSArray arrayWithObjects:@"-c",terminalCode, nil];
            [task setArguments: arguments];
            
            NSPipe *pipe;
            pipe = [NSPipe pipe];
            [task setStandardOutput: pipe];
            
            NSFileHandle *file;
            file = [pipe fileHandleForReading];
            
            [task launch];
            
            NSData *data;
            data = [file readDataToEndOfFile];
            
            NSString *string;
            string = [[NSString alloc] initWithData: data
                                           encoding: NSUTF8StringEncoding];
            if ([string containsString:@"0x"]) {
                continue;
            }
            NSLog (@"\n==============%d==============\n%@\n\n", i, string);
            
        }
        
    }
    return 0;
}
